<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <productcategorySearch @search="reload"  />
    <el-card shadow="never">
     
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        
        :parse-data="parseData"
        @done="onDone"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
          
        </template>
        <template v-slot:icon="{ row }">
          <img :src="row.iconUrl" min-width="70" height="70"/>
        </template>
        <!-- 标题列 -->
        <template slot="title" slot-scope="{ row }">
          <i :class="row.icon"></i> {{ row.name }}
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            v-if="row.parentId==null||row.parentId==''"
            @click="openEdit({parentId:row.id,parentName:row.name})"
          >
            添加
          </el-link>
       
          <el-link 
              type="primary"
     
              :underline="false"
              icon="el-icon-star-on"
              @click="setIsDefault(row)"
              v-if="row.isDefault==0&&(row.parentId==null||row.parentId=='')"
          >
              推荐首页
          </el-link>
          <el-link
            type="danger"
            slot="reference"
            :underline="false"
            icon="el-icon-star-off"
            v-if="row.isDefault==1&&(row.parentId==null||row.parentId=='')"
            @click="setIsDefault(row)"
           >
            取消推荐
        </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
          >
            <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
            >
                删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <productcategoryEdit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
      
    />
    <!-- 导出数据 -->
 
  </div>
</template>

<script>
  import productcategoryEdit from './components/productcategory-edit';
  import productcategorySearch from './components/productcategory-search';
  import {QueryProductCategroy,SetCategroyIsDefault,Deleted } from '@/api/productmanager/productcategorytmanager';
  export default {
    name: 'ProductCategroy',
    components: {productcategoryEdit,productcategorySearch},
    data() {
      return {
   
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 60,
            align: 'center',
            label: '序号',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'name',
            label: '分类名称',
            minWidth: 100,
            sortable: 'name',
            showOverflowTooltip: true,
          },

          {
            prop: 'code',
            label: '分类编号',
            sortable: 'code',
            showOverflowTooltip: true,
          },
          {
            prop: 'iconUrl',
            label: '图标',
            showOverflowTooltip: true,
            showOverflowTooltip: true, 
            width: 120,
            slot: 'icon'
           // avatar: 'http://cdn.eleadmin.com/20200609/c184eef391ae48dba87e3057e70238fb.jpg'
          },
          
          {
            prop: 'isDefaultDesc',
            label: '是否推荐',
            align: 'center',
            width: 80,
            showOverflowTooltip: true,
          },
          
          {
            prop: 'createUserName',
            label: '创建人',
            showOverflowTooltip: true,
            minWidth: 80,
            align: 'center',
            sortable: 'createUserName',
          },
          {
            prop: 'createDate',
            label: '创建时间',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center',
            sortable: 'createDate',
            formatter: (row, column, cellValue) => {
              return this.$util.toDateString(cellValue,"YYYY-MM-DD");
            }
          },
          {
            prop: 'statusDesc',
            label: '状态',
            showOverflowTooltip: true,
            width: 60,
            align: 'center'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 280,
            align: 'center',
            resizable: false,
            slot: 'action',
            hideInSetting: true,
            showOverflowTooltip: true
          }
        ],

        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 全部菜单数据
        // 导出列
        exportList: [
          { name: "分类名称", keys: "name", status: true },
          { name: "分类代码", keys: "code", status: true },
          {
            prop: 'createDate', label: '创建时间', showOverflowTooltip: true,  align: 'left',sortable: 'createDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
          { name: "创建人", keys: "createUserName", status: true },
          { name: "状态", keys: "statusDesc", status: true },
        ]
      };
    },
    methods: {
      /* 表格数据源 */
      datasource(params) {
       
      
         params.where.pageindex = params.page;
         params.where.pagesize = 0;
        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }
      
        return  QueryProductCategroy(params.where);
    
      },

      /* 数据转为树形结构 */
      parseData(data) {
        return this.$util.toTreeData({
          data: data,
          idField: 'id',
          parentIdField: 'parentId'
        });
      },
      /* 表格渲染完成回调 */
      onDone({ data }) {
           // 编辑弹窗编辑完成后 done 事件回传表单数据，使用 Object.assign 赋值给 current 即可修改表格的行数据
           if(this.current) {
            Object.assign(this.current, data);
          }
        },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ where: where });
      },
      
      /**
       * 显示编辑
       * @param row 当前行对像
       */
      openEdit(row) {
      console.log("openEdit",row);
        this.current = null;
        this.current = row;
        this.showEdit = true;
      },
      setIsDefault(row){
        const loading = this.$loading({ lock: true });
         let isdefault;
         if(row.isDefault==1){
           isdefault=0;
         }else{
          isdefault=1;
         }
         SetCategroyIsDefault(row.id,isdefault)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
         
      },
      /* 删除 */
      remove(row) {
        if (row.hasChild) {
          this.$message.error('请先删除子节点');
          return;
        }
        const loading = this.$loading({ lock: true });
        Deleted(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      }
     
    }
  };
</script>
